package com.ciwong.epaper.modules.epaper.util;

import com.ciwong.epaper.modules.epaper.bean.CatalogueInfo;
import com.ciwong.epaper.modules.epaper.bean.Module;
import com.ciwong.epaper.modules.epaper.bean.ModuleContent;
import com.ciwong.epaper.modules.epaper.bean.ModuleInfo;
import com.ciwong.epaper.modules.me.bean.WorkContents;
import com.ciwong.epaper.util.download.DownLoadInfo;

import java.util.List;

/**
 * Created by keepon on 2018/7/18.
 */
public class HomeWorkUtil {


	/**
	 * 获取Module
	 *
	 * @param modules
	 * @param workContent
	 * @return
	 */
	public static Module getModule(List<Module> modules, WorkContents workContent) {
		Module mModule = null;
		Module wordModule = null;
		try {
			//这里要循环查询2遍 因为第一次要全部以version查询 没有结果的情况下 才采用单词的特殊情况
			for (Module module : modules) {
				if (module.getModuleInfo().getModuleId() == workContent.getModuleId()) {
					if (module != null) {
						List<ModuleContent> moduleContents = module.getResourceList();
						for (int index = 0; index < moduleContents.size(); index++) {
							ModuleContent moduleContent = moduleContents.get(index);
							//为了解决单词跟读本地资源找不到问题，服务器返回数据有两个特征getVersionId  跟getParentVersionId是一样，本地资源 VersionId 是0
							//句子的新工具父版本号改为0  老资源父版本号不为0   单词版本号为0  父版本号不为0
							if (module.getModuleInfo().getModuleId() == ModuleInfo.ModuleInfoId.MODULE_ID_REPEAT || module.getModuleInfo().getModuleId() == ModuleInfo.ModuleInfoId.MODULE_ID_DICTATION) {
								if (moduleContent.getVersionId().equals(workContent.getVersionId()) && !moduleContent.getVersionId().equals("0")) {//除了单词不能用versionId唯一确定资源 其他都能确定  并且version不能等于0
									mModule = module;
								} else if (moduleContent.getParentVersionId().equals(workContent.getParentVersionId()) && moduleContent.getVersionId().equals("0")) {
									wordModule = module;
								}
							} else {
								if (moduleContent.getVersionId().equals(workContent.getVersionId()) && moduleContent.getParentVersionId().equals(workContent.getParentVersionId())) {
									mModule = module;
								}
							}
						}
					}
				}
			}
			if (mModule == null) {
				mModule = wordModule;
			}

			//解决句子资源127，接口给10打不开问题，这时需要重新找一遍
			if (mModule == null) {
				for (Module module : modules) {
					if (module.getModuleInfo().getModuleId() == 127 || module.getModuleInfo().getModuleId() == 10 || module.getModuleInfo().getModuleId() == ModuleInfo.ModuleInfoId.MODULE_ID_DICTATION) {
						if (module != null) {
							List<ModuleContent> moduleContents = module.getResourceList();
							for (int index = 0; index < moduleContents.size(); index++) {
								ModuleContent moduleContent = moduleContents.get(index);
								//为了解决单词跟读本地资源找不到问题，服务器返回数据有两个特征getVersionId  跟getParentVersionId是一样，本地资源 VersionId 是0
								//句子的新工具父版本号改为0  老资源父版本号不为0   单词版本号为0  父版本号不为0
								if (module.getModuleInfo().getModuleId() == ModuleInfo.ModuleInfoId.MODULE_ID_REPEAT) {
									if (moduleContent.getVersionId().equals(workContent.getVersionId()) && !moduleContent.getVersionId().equals("0")) {//除了单词不能用versionId唯一确定资源 其他都能确定  并且version不能等于0
										mModule = module;
									} else if (moduleContent.getParentVersionId().equals(workContent.getParentVersionId()) && moduleContent.getVersionId().equals("0")) {
										wordModule = module;
									}
								} else {
									if (moduleContent.getVersionId().equals(workContent.getVersionId()) && moduleContent.getParentVersionId().equals(workContent.getParentVersionId())) {
										mModule = module;
									}
								}
							}
						}
					}
				}
			}
			if (mModule == null) {
				mModule = wordModule;
			}

		} catch (Exception e) {
			e.getStackTrace();
		}
		return mModule;
	}

	/**
	 * 获取ModuleIndex
	 *
	 * @param module
	 * @param workContent
	 * @return
	 */
	public static int getModuleContentIndex(Module module, WorkContents workContent) {
		try {
			int mIndex = -1;
			int wordIndex = -1;
			if (module != null) {
				List<ModuleContent> moduleContents = module.getResourceList();
				for (int index = 0; index < moduleContents.size(); index++) {
					ModuleContent moduleContent = moduleContents.get(index);

					//为了解决单词跟读本地资源找不到问题，服务器返回数据有两个特征getVersionId  跟getParentVersionId是一样，本地资源 VersionId 是0
					if (module.getModuleInfo().getModuleId() == ModuleInfo.ModuleInfoId.MODULE_ID_REPEAT || module.getModuleInfo().getModuleId() == ModuleInfo.ModuleInfoId.MODULE_ID_DICTATION) {
						if (moduleContent.getVersionId().equals(workContent.getVersionId()) && !moduleContent.getVersionId().equals("0")) {//除了单词不能用versionId唯一确定资源 其他都能确定
							mIndex = index;
						} else if (moduleContent.getParentVersionId().equals(workContent.getParentVersionId()) && moduleContent.getVersionId().equals("0")) {//单词
							wordIndex = index;
						}

					} else if (moduleContent.getVersionId().equals(workContent.getVersionId()) && moduleContent.getParentVersionId().equals(workContent.getParentVersionId())) {
						mIndex = index;
					}
				}
			}
			if (mIndex == -1) {
				mIndex = wordIndex;
			}
			return mIndex;
		} catch (Exception e) {
			e.getStackTrace();
		}
		return -1;
	}

	public static void matchCurrentDownloadingHomework(DownLoadInfo currentDownLoadInfo, WorkContents currentWorkContents, ResoureTypeMatchListener resoureTypeMatchListener) {
		try {
			if (currentDownLoadInfo.getType() == CatalogueInfo.PackageMode.PACKAGE_MODE_NEW) {
				//如果是新包需要对比ModuleId和VersionId,PackageId和ChapterId
				if (currentDownLoadInfo.getBookId().equals(currentWorkContents.getPackageId()) && currentDownLoadInfo.getChapterId().equals(currentWorkContents.getcId()) && currentDownLoadInfo.getModuleId().equals(currentWorkContents.getModuleId() + "") && currentDownLoadInfo.getVersionId().equals(currentWorkContents.getVersionId())) {
					resoureTypeMatchListener.homeworkMatchSuccess(currentDownLoadInfo);
				} else {
					resoureTypeMatchListener.homeworkMatchFailed();
				}
			} else {
				//如果是旧包只需要对比PackageId和ChapterId
				if (currentDownLoadInfo.getBookId().equals(currentWorkContents.getPackageId()) && currentDownLoadInfo.getChapterId().equals(currentWorkContents.getcId())) {
					resoureTypeMatchListener.homeworkMatchSuccess(currentDownLoadInfo);
				} else {
					resoureTypeMatchListener.homeworkMatchFailed();
				}
			}
		} catch (Exception e) {
			resoureTypeMatchListener.homeworkMatchFailed();
		}

	}

	public interface ResoureTypeMatchListener {
		void homeworkMatchSuccess(DownLoadInfo downLoadInfo);

		void homeworkMatchFailed();
	}

	public static boolean isDownloadInfoTheSameToClickDownLoadInfo(DownLoadInfo downLoadInfo, DownLoadInfo currentClickDownloadInfo) {
		try {
			return downLoadInfo.getBookId().equals(currentClickDownloadInfo.getBookId()) && downLoadInfo.getChapterId().equals(currentClickDownloadInfo.getChapterId()) && downLoadInfo.getModuleId().equals(currentClickDownloadInfo.getModuleId() + "") && downLoadInfo.getVersionId().equals(currentClickDownloadInfo.getVersionId());
		} catch (Exception e) {
			return false;
		}
	}
}